/*
	Convert voxels block to height map
*/

#include "voxel_1.cl"
#include "height_map_common.cl"


kernel void Main (read_only image2d_t inputImage, write_only image2d_t outputHeightMap, int4 blockCoord)
{
	const int3	coord3d		= GetGlobalCoord();
	const int3	global_coord = blockCoord.xyz + coord3d;
	const int2	out_coord	= ToHeightMapCoords( global_coord );

	const float	height_step = 1.0f / (float)(VOXEL_TEXTURE_BLOCK_SIZE_Z);

	float	height = 1.0f;
	uint	color  = COLOR_GREEN;

	#pragma unroll
	for (int z = VOXEL_TEXTURE_BLOCK_SIZE_Z-1; z >= 0; --z)
	{
		const int3 coord = coord3d + (int3)(0, 0, z);

		Voxel voxel = ReadVoxel( inputImage, GetTexCoord( coord ) );

		if ( voxel.type != EMPTY )
			break;

		height -= height_step;
	}
	
	WriteToHeightMap( outputHeightMap, out_coord, height, color );
}
